#include "../include/N_arytree.h"

int delete_dir(t_node** root,char*path)
{	
	t_node * current,t,t1;
	char **tokens=NULL;
	int count,i,flag=1;
	count=generate_tokens(&tokens,path,'/');
       
	
	
	i = 0;
	current = *root;
        
	//Traversing the source dir
	while(current != NULL)
	{

		if(current->is_deleted == 0 && strcmp(current->data->file_name,tokens[i]) == 0 && current->data->fileType=='D')
		{
			i++;
			if(i == count)
				break;
			t=*current;
			current = current->left_child;
  
                      
			
		}
		//Goto right siblings
		else if(current->right_siblings != NULL)
		{	
			
			current = current->right_siblings;
		}
		else
		{
			flag =0;
			break;
		}
	}
	
	
	//Invalid positions
	if(flag == 0||current == NULL)
	{
		//#define ERR_VFS_DELETEDIR_01 <CANNOT_FIND_SPECIFIED_DIR>"
		return 1;
	}
	
	else if(current->left_child !=NULL)
	{
		//#define ERR_VFS_DELETEDIR_02 <DIRECTORY_IS_NOT_EMPTY>"
		return 2;
	}
	
	fd* f = current->data;
	char*fullpath;
	fullpath=(char*)malloc(sizeof(char)*(strlen(f->path)+strlen(f->file_name)+1));
	strcpy(fullpath,f->path);
	strcat(fullpath,"/");
	strcat(fullpath,f->file_name);
	
	//printf("%s",fullpath);

	hashDelete(hash,*f);
	bstDeleteNode(&root,fullpath);
	current->is_deleted = 1;
	
	fdcounter--;
	return 0;
}
